package com.ambitor.ipro.labelParser;

import com.ambitor.grass.sql.dao.SuperDao;
import com.ambitor.grass.util.data.DataMap;
import com.ambitor.grass.util.data.IData;
import com.ambitor.ipro.exceptions.ProException;
import com.ambitor.ipro.service.TopService;
import com.ambitor.ipro.util.ResultCode;

public class IntellLabelParserFactory {

	public static IntellLabelParser instance(String intellLabel,
			SuperDao superDao, TopService topService, IData userSession,
			String installIid, int num) throws ProException {
		String strategyClass = getStrategyClassName(superDao, intellLabel);
		IntellLabelParser parser = null;
		try {
			Class<?> clazz = Class.forName(strategyClass);
			parser = (IntellLabelParser) clazz.newInstance();
		} catch (Exception e) {
			throw new ProException(ResultCode.EXCEPTION,
					"动态生成IntellLabelParser出错", e);
		}
		parser.setSuperDao(superDao);
		parser.setTopService(topService);
		parser.setUserSession(userSession);
		parser.setInstallIid(installIid);
		parser.setNum(num);
		return parser;
	}

	@SuppressWarnings("unchecked")
	private static String getStrategyClassName(SuperDao superDao,
			String intellLabel) throws ProException {
		IData param = new DataMap();
		param.put("INTELL_LABEL", intellLabel);
		IData intellMap = null;
		try {
			intellMap = superDao.queryByPK("TD_P_INTELL", "STRATEGY_CLASS",
					param);
		} catch (Exception e) {
			throw new ProException(ResultCode.DB_EXCEPTION, "查询TD_P_INTELL出错",
					e);
		}
		String strategyClassName = "com.ambitor.ipro.labelParser."
				+ intellMap.getString("STRATEGY_CLASS");
		return strategyClassName;
	}
}
